import {
	ajax, getTheme, viewModel
} from "nc-lightapp-front";
let { setGlobalStorage, getGlobalStorage, removeGlobalStorage } = viewModel;

export function requestPromise(payload) {
	return new Promise((resolve, reject) => {
		const config = {
			...payload,
			method: "post",
			success: response => {
				if (response.success) {
					resolve(response);
				} else {
					reject(response)
				}
			},
		}

		if (payload.hasErrCallBack) {
			config.error =  (...args) => reject(...args)
		}

		ajax(config);
	})
}

export function setStatePromise(obj) {
	return new Promise((resolve /* , reject */ ) => {
		this.setState(obj, () => {
			resolve()
		})
	})
}

export function nccGetTheme() {
	if(getTheme) {
		return getTheme()
	}
	const THEME_TYPE = (window && window.nccColor) || (window && window.top && window.top.nccColor) || getGlobalStorage('localStorage', 'nccColor')
	return THEME_TYPE
}